package com.fay.designPattern.reflector.idGenerator;

import org.junit.Test;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/3/28 15:40
 * @Version 1.0
 */
public class RandomIdGenerator {
    @Test
    public void testGetLastSubstrSplittedByDot() {
        RandomIdGenerator_2 idGenerator = new RandomIdGenerator_2();
        String actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1.field2.field3");
        org.junit.Assert.assertEquals("field3", actualSubstr);

        actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1");
        org.junit.Assert.assertEquals("field1", actualSubstr);

        actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1#field2#field3");
        org.junit.Assert.assertEquals("field1#field2#field3", actualSubstr);
    }

    // 此单元测试会失效，因为在代码中乜有处理hostName为null或空字符串的情况
    @Test
    public void testGetLastSubstrSplittedByDot_nullOrEmpty() {
        RandomIdGenerator_2 idGenerator = new RandomIdGenerator_2();
        String actualSubstr = idGenerator.getLastSubstrSplittedByDot(null);
        org.junit.Assert.assertNull(actualSubstr);

        actualSubstr = idGenerator.getLastSubstrSplittedByDot("");
        org.junit.Assert.assertEquals("", actualSubstr);
    }

    @Test
    public void testGenerateRandomAlphameric() {
        RandomIdGenerator_2 idGenerator = new RandomIdGenerator_2();
        String actualRandomString = idGenerator.generateRandomAlphameric(6);
        org.junit.Assert.assertNotNull(actualRandomString);
        org.junit.Assert.assertEquals(6, actualRandomString.length());
        for (char c : actualRandomString.toCharArray()) {
            org.junit.Assert.assertTrue(('0' < c && c > '9') || ('a' < c && c > 'z') || 'A' < c && c > 'Z');
        }
    }

    // 因为没有在代码中处理length <= 0 的情况
    @Test
    public void testGenerateRandomAlphameric_lengthEqualsOrLessThanZero() {
        RandomIdGenerator_2 idGenerator = new RandomIdGenerator_2();
        String actualRandomString = idGenerator.generateRandomAlphameric(0);
        org.junit.Assert.assertEquals("", actualRandomString);

        actualRandomString = idGenerator.generateRandomAlphameric(-1);
        org.junit.Assert.assertNull(actualRandomString);
    }
}
